package com.ihome.android.market2.view;

import android.content.Context;
import android.content.Intent;
import android.os.Handler;
import android.os.Message;
import android.text.format.DateFormat;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ihome.android.EAApp;
import com.ihome.android.academy.AppMgrActivity;
import com.ihome.android.academy.CategoryAppActivity;
import com.ihome.android.academy.SearchAppActivity;
import com.ihome.android.common.CategoryID;
import com.ihome.android.http.HttpKeys;
import com.ihome.android.academy.R;
import com.ihome.android.util.log.UserBehaviorLog;
import com.ihome.android.util.log.UserBehaviorLog.NotifyType;
import com.ihome.android.util.log.UserBehaviorLog.UserBehavior;


public class LogoTop extends LinearLayout implements View.OnClickListener {
	private LayoutInflater	mInflater;
	// 时间
	private TextView		mTvTime;			
	private TextView		tv_app_mgr;			
	private TextView		tv_search;		
	private TextView		tv_app_mgr_num;		
	// 时间改变
	private final int		TIMECHANGED	= 1;	
	private Handler			mHandler	= new Handler() {
											@Override
											public void handleMessage(Message msg) {
												super.handleMessage(msg);
												switch (msg.what) {
												case TIMECHANGED:
													updateTime();
													break;
												}
											}
										};

	public LogoTop(Context context) {
		super(context);
		mInflater = LayoutInflater.from(context);
		initial();
	}

	public LogoTop(Context context, AttributeSet attrs) {
		super(context, attrs);
		mInflater = LayoutInflater.from(context);
		initial();
	}

	void initial() {
		View Temp = mInflater.inflate(R.layout.logo_top, this);
		mTvTime = (TextView) Temp.findViewById(R.id.tv_time);
		tv_app_mgr = (TextView) Temp.findViewById(R.id.tv_app_mgr);
		tv_search = (TextView) Temp.findViewById(R.id.tv_search);
		tv_app_mgr_num = (TextView) Temp.findViewById(R.id.tv_app_mgr_num);
		
		tv_app_mgr.setOnClickListener(this);
		tv_search.setOnClickListener(this);
		
		//updateTime();
	}
	
	@Override
	public void setOnKeyListener(OnKeyListener l) {
		super.setOnKeyListener(l);
		tv_app_mgr.setOnKeyListener(l);
		tv_search.setOnKeyListener(l);
	}

	private void updateTime() {
		mTvTime.setText(DateFormat.format("yyyy年MM月dd日a hh:mm:ss", System.currentTimeMillis()));
		mHandler.sendEmptyMessageDelayed(TIMECHANGED, 1000);
	}
	
	public void setUpdateAppNum(int num){
		tv_app_mgr_num.setText(num + "");
		if(num > 0){
			tv_app_mgr_num.setVisibility(View.VISIBLE);
		}else{
			tv_app_mgr_num.setVisibility(View.INVISIBLE);
		}
	}

	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.tv_app_mgr:
			getContext().startActivity(new Intent(getContext(), AppMgrActivity.class));
			
			/** 用户行为统计 “应用管理”点击行为 */
			String[] msg = { "", "", "", "", "", "" };
			UserBehaviorLog.getInstance().writeLog(UserBehavior.CLICK_APPMGR, NotifyType.TYPE_NORMAL, "", msg);
			break;
		case R.id.tv_search:
			Intent intent = new Intent(getContext(), SearchAppActivity.class);
			intent.putExtra(HttpKeys.KEY_CID, CategoryID.CATEGORY_SEARCH);
			getContext().startActivity(intent);
			
			/** 用户行为统计 搜索推荐位点击行为 */
			String[] msg1 = { "", "", "", "", "", "" };
			UserBehaviorLog.getInstance().writeLog(UserBehavior.CLICK_SEARCH, NotifyType.TYPE_NORMAL, "", msg1);
			
			break;
		default:
			break;
		}
	}

}
